home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / nt / gd25s.zip / UTIL.C < prev    next >
C/C++ Source or Header  |  1993-10-08  |  19KB  |  771 lines

  1. /* Support routines for GNU DIFF.
  2.    Copyright (C) 1988, 1989, 1992, 1993 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU DIFF.
  5.  
  6. GNU DIFF is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU DIFF is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU DIFF; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "diff.h"
  21.  
  22. /* Queue up one-line messages to be printed at the end,
  23.    when -l is specified.  Each message is recorded with a `struct msg'.  */
  24.  
  25. struct msg
  26. {
  27.   struct msg *next;
  28.   char const *format;
  29.   char const *arg1;
  30.   char const *arg2;
  31.   char const *arg3;
  32.   char const *arg4;
  33. };
  34.  
  35. /* Head of the chain of queues messages.  */
  36.  
  37. static struct msg *msg_chain;
  38.  
  39. /* Tail of the chain of queues messages.  */
  40.  
  41. static struct msg **msg_chain_end = &msg_chain;
  42.  
  43. /* Use when a system call returns non-zero status.
  44.    TEXT should normally be the file name.  */
  45.  
  46. void
  47. perror_with_name (text)
  48.      char const *text;
  49. {
  50.   int e = errno;
  51.   fprintf (stderr, "%s: ", program);
  52.   errno = e;
  53.   perror (text);
  54. }
  55.  
  56. /* Use when a system call returns non-zero status and that is fatal.  */
  57.  
  58. void
  59. pfatal_with_name (text)
  60.      char const *text;
  61. {
  62.   int e = errno;
  63.   print_message_queue ();
  64.   fprintf (stderr, "%s: ", program);
  65.   errno = e;
  66.   perror (text);
  67.   exit (2);
  68. }
  69.  
  70. /* Print an error message from the format-string FORMAT
  71.    with args ARG1 and ARG2.  */
  72.  
  73. void
  74. error (format, arg, arg1)
  75.      char const *format, *arg, *arg1;
  76. {
  77.   fprintf (stderr, "%s: ", program);
  78.   fprintf (stderr, format, arg, arg1);
  79.   fprintf (stderr, "\n");
  80. }
  81.  
  82. /* Print an error message containing the string TEXT, then exit.  */
  83.  
  84. void
  85. fatal (m)
  86.      char const *m;
  87. {
  88.   print_message_queue ();
  89.   error ("%s", m, 0);
  90.   exit (2);
  91. }
  92.  
  93. /* Like printf, except if -l in effect then save the message and print later.
  94.    This is used for things like "binary files differ" and "Only in ...".  */
  95.  
  96. void
  97. message (format, arg1, arg2)
  98.      char const *format, *arg1, *arg2;
  99. {
  100.   message5 (format, arg1, arg2, 0, 0);
  101. }
  102.  
  103. void
  104. message5 (format, arg1, arg2, arg3, arg4)
  105.      char const *format, *arg1, *arg2, *arg3, *arg4;
  106. {
  107.   if (paginate_flag)
  108.     {
  109.       struct msg *new = (struct msg *) xmalloc (sizeof (struct msg));
  110.       new->format = format;
  111.       new->arg1 = concat (arg1, "", "");
  112.       new->arg2 = concat (arg2, "", "");
  113.       new->arg3 = arg3 ? concat (arg3, "", "") : 0;
  114.       new->arg4 = arg4 ? concat (arg4, "", "") : 0;
  115.       new->next = 0;
  116.       *msg_chain_end = new;
  117.       msg_chain_end = &new->next;
  118.     }
  119.   else
  120.     {
  121.       if (sdiff_help_sdiff)
  122.     putchar (' ');
  123.       printf (format, arg1, arg2, arg3, arg4);
  124.     }
  125. }
  126.  
  127. /* Output all the messages that were saved up by calls to `message'.  */
  128.  
  129. void
  130. print_message_queue ()
  131. {
  132.   struct msg *m;
  133.  
  134.   for (m = msg_chain; m; m = m->next)
  135.     printf (m->format, m->arg1, m->arg2, m->arg3, m->arg4);
  136. }
  137.  
  138. /* Call before outputting the results of comparing files NAME0 and NAME1
  139.    to set up OUTFILE, the stdio stream for the output to go to.
  140.  
  141.    Usually, OUTFILE is just stdout.  But when -l was specified
  142.    we fork off a `pr' and make OUTFILE a pipe to it.
  143.    `pr' then outputs to our stdout.  */
  144.  
  145. static char const *current_name0;
  146. static char const *current_name1;
  147. static int current_depth;
  148.  
  149. void
  150. setup_output (name0, name1, depth)
  151.      char const *name0, *name1;
  152.      int depth;
  153. {
  154.   current_name0 = name0;
  155.   current_name1 = name1;
  156.   current_depth = depth;
  157.   outfile = 0;
  158. }
  159.  
  160. static pid_t pr_pid;
  161.  
  162. void
  163. begin_output ()
  164. {
  165.   char *name;
  166.  
  167.   if (outfile != 0)
  168.     return;
  169.  
  170.   /* Construct the header of this piece of diff.  */
  171.   name = xmalloc (strlen (current_name0) + strlen (current_name1)
  172.           + strlen (switch_string) + 7);
  173.   /* Posix.2 section 4.17.6.1.1 specifies this format.  But there are some
  174.      bugs in the first printing (IEEE Std 1003.2-1992 p 251 l 3304):
  175.      it says that we must print only the last component of the pathnames,
  176.      and it requires two spaces after "diff" if there are no options.
  177.      These requirements are silly and do not match historical practice.  */
  178.   sprintf (name, "diff%s %s %s", switch_string, current_name0, current_name1);
  179.  
  180.   if (paginate_flag)
  181.     {
  182. #if defined(__MSDOS__) || defined(__NT__)
  183.       char command[120];
  184.  
  185.       sprintf(command, "%s -f -h \"%s\"", PR_FILE_NAME, name);
  186.       if ((outfile = popen(command, "w")) == NULL)
  187.         pfatal_with_name ("popen");
  188. #else
  189.       int pipes[2];
  190.  
  191.       /* Fork a `pr' and make OUTFILE a pipe to it.  */
  192.       if (pipe (pipes) < 0)
  193.     pfatal_with_name ("pipe");
  194.  
  195.       fflush (stdout);
  196.  
  197.       pr_pid = vfork ();
  198.       if (pr_pid < 0)
  199.     pfatal_with_name ("vfork");
  200.  
  201.       if (pr_pid == 0)
  202.     {
  203.       close (pipes[1]);
  204.       if (pipes[0] != STDIN_FILENO)
  205.         {
  206.           if (dup2 (pipes[0], STDIN_FILENO) < 0)
  207.         pfatal_with_name ("dup2");
  208.           close (pipes[0]);
  209.         }
  210.  
  211.       execl (PR_FILE_NAME, PR_FILE_NAME, "-f", "-h", name, 0);
  212.       pfatal_with_name (PR_FILE_NAME);
  213.     }
  214.       else
  215.     {
  216.       close (pipes[0]);
  217.       outfile = fdopen (pipes[1], "w");
  218.     }
  219. #endif /*__MSDOS__||__NT__*/
  220.     }
  221.   else
  222.     {
  223.  
  224.       /* If -l was not specified, output the diff straight to `stdout'.  */
  225.  
  226.       outfile = stdout;
  227.  
  228.       /* If handling multiple files (because scanning a directory),
  229.      print which files the following output is about.  */
  230.       if (current_depth > 0)
  231.     printf ("%s\n", name);
  232.     }
  233.  
  234.   free (name);
  235.  
  236.   /* A special header is needed at the beginning of context output.  */
  237.   switch (output_style)
  238.     {
  239.     case OUTPUT_CONTEXT:
  240.       print_context_header (files, 0);
  241.       break;
  242.  
  243.     case OUTPUT_UNIFIED:
  244.       print_context_header (files, 1);
  245.       break;
  246.  
  247.     default:
  248.       break;
  249.     }
  250. }
  251.  
  252. /* Call after the end of output of diffs for one file.
  253.    Close OUTFILE and get rid of the `pr' subfork.  */
  254.  
  255. void
  256. finish_output ()
  257. {
  258.   if (outfile != 0 && outfile != stdout)
  259.     {
  260. #if defined(__MSDOS__) || defined(__NT__)
  261.       if (pclose (outfile))
  262.     pfatal_with_name ("write error");
  263. #else
  264.       int wstatus;
  265.       if (ferror (outfile))
  266.     fatal ("write error");
  267.       if (fclose (outfile) != 0)
  268.     pfatal_with_name ("write error");
  269. #if HAVE_WAITPID
  270.       if (waitpid (pr_pid, &wstatus, 0) < 0)
  271.     pfatal_with_name ("waitpid");
  272. #else
  273.       for (;;) {
  274.     pid_t w = wait (&wstatus);
  275.     if (w < 0)
  276.       pfatal_with_name ("wait");
  277.     if (w == pr_pid)
  278.       break;
  279.       }
  280. #endif
  281.       if (! WIFEXITED (wstatus) || WEXITSTATUS (wstatus) != 0)
  282.     fatal ("subsidiary pr failed");
  283. #endif /*__MSDOS__||__NT__*/
  284.     }
  285.  
  286.   outfile = 0;
  287. }
  288.  
  289. /* Compare two lines (typically one from each input file)
  290.    according to the command line options.
  291.    Return 1 if the lines differ, like `memcmp'.  */
  292.  
  293. int
  294. line_cmp (s1, len1, s2, len2)
  295.      char const HUGE *s1, HUGE *s2;
  296.      size_t len1, len2;
  297. {
  298.   register unsigned char const *t1, *t2;
  299.   register unsigned char end_char = line_end_char;
  300.  
  301.   /* Check first for exact identity.
  302.      If that is true, return 0 immediately.
  303.      This detects the common case of exact identity
  304.      faster than complete comparison would.  */
  305.  
  306.   if (len1 == len2 && memcmp (s1, s2, len1) == 0)
  307.     return 0;
  308.  
  309.   /* Not exactly identical, but perhaps they match anyway
  310.      when case or white space is ignored.  */
  311.  
  312.   if (ignore_case_flag | ignore_space_change_flag | ignore_all_space_flag)
  313.     {
  314.       t1 = (unsigned char const *) s1;
  315.       t2 = (unsigned char const *) s2;
  316.  
  317.       while (1)
  318.     {
  319.       register unsigned char c1 = *t1++;
  320.       register unsigned char c2 = *t2++;
  321.  
  322.       /* Ignore horizontal white space if -b or -w is specified.  */
  323.  
  324.       if (ignore_all_space_flag)
  325.         {
  326.           /* For -w, just skip past any white space.  */
  327.           while (isspace (c1) && c1 != end_char) c1 = *t1++;
  328.           while (isspace (c2) && c2 != end_char) c2 = *t2++;
  329.         }
  330.       else if (ignore_space_change_flag)
  331.         {
  332.           /* For -b, advance past any sequence of white space in line 1
  333.          and consider it just one Space, or nothing at all
  334.          if it is at the end of the line.  */
  335.           if (isspace (c1))
  336.         {
  337.           while (c1 != end_char)
  338.             {
  339.               c1 = *t1++;
  340.               if (! isspace (c1))
  341.             {
  342.               --t1;
  343.               c1 = ' ';
  344.               break;
  345.             }
  346.             }
  347.         }
  348.  
  349.           /* Likewise for line 2.  */
  350.           if (isspace (c2))
  351.         {
  352.           while (c2 != end_char)
  353.             {
  354.               c2 = *t2++;
  355.               if (! isspace (c2))
  356.             {
  357.               --t2;
  358.               c2 = ' ';
  359.               break;
  360.             }
  361.             }
  362.         }
  363.         }
  364.  
  365.       /* Upcase all letters if -i is specified.  */
  366.  
  367.       if (ignore_case_flag)
  368.         {
  369.           if (islower (c1))
  370.         c1 = toupper (c1);
  371.           if (islower (c2))
  372.         c2 = toupper (c2);
  373.         }
  374.  
  375.       if (c1 != c2)
  376.         break;
  377.       if (c1 == end_char)
  378.         return 0;
  379.     }
  380.     }
  381.  
  382.   return (1);
  383. }
  384.  
  385. /* Find the consecutive changes at the start of the script START.
  386.    Return the last link before the first gap.  */
  387.  
  388. struct change *
  389. find_change (start)
  390.      struct change *start;
  391. {
  392.   return start;
  393. }
  394.  
  395. struct change *
  396. find_reverse_change (start)
  397.      struct change *start;
  398. {
  399.   return start;
  400. }
  401.  
  402. /* Divide SCRIPT into pieces by calling HUNKFUN and
  403.    print each piece with PRINTFUN.
  404.    Both functions take one arg, an edit script.
  405.  
  406.    HUNKFUN is called with the tail of the script
  407.    and returns the last link that belongs together with the start
  408.    of the tail.
  409.  
  410.    PRINTFUN takes a subscript which belongs together (with a null
  411.    link at the end) and prints it.  */
  412.  
  413. void
  414. print_script (script, hunkfun, printfun)
  415.      struct change *script;
  416.      struct change * (*hunkfun) PARAMS((struct change *));
  417.      void (*printfun) PARAMS((struct change *));
  418. {
  419.   struct change *next = script;
  420.  
  421.   while (next)
  422.     {
  423.       struct change *this, *end;
  424.  
  425.       /* Find a set of changes that belong together.  */
  426.       this = next;
  427.       end = (*hunkfun) (next);
  428.  
  429.       /* Disconnect them from the rest of the changes,
  430.      making them a hunk, and remember the rest for next iteration.  */
  431.       next = end->link;
  432.       end->link = 0;
  433. #ifdef DEBUG
  434.       debug_script (this);
  435. #endif
  436.  
  437.       /* Print this hunk.  */
  438.       (*printfun) (this);
  439.  
  440.       /* Reconnect the script so it will all be freed properly.  */
  441.       end->link = next;
  442.     }
  443. }
  444.  
  445. /* Print the text of a single line LINE,
  446.    flagging it with the characters in LINE_FLAG (which say whether
  447.    the line is inserted, deleted, changed, etc.).  */
  448.  
  449. void
  450. print_1_line (line_flag, line)
  451.      char const *line_flag;
  452.      char const HUGE * const *line;
  453. {
  454.   char const HUGE *text = line[0], HUGE *limit = line[1]; /* Help the compiler.  */
  455.   FILE *out = outfile; /* Help the compiler some more.  */
  456.   char const *flag_format = 0;
  457.  
  458.   /* If -T was specified, use a Tab between the line-flag and the text.
  459.      Otherwise use a Space (as Unix diff does).
  460.      Print neither space nor tab if line-flags are empty.  */
  461.  
  462.   if (line_flag && *line_flag)
  463.     {
  464.       flag_format = tab_align_flag ? "%s\t" : "%s ";
  465.       fprintf (out, flag_format, line_flag);
  466.     }
  467.  
  468.   output_1_line (text, limit, flag_format, line_flag);
  469.  
  470.   if ((!line_flag || line_flag[0]) && limit[-1] != '\n'
  471.       && line_end_char == '\n')
  472.     fprintf (out, "\n\\ No newline at end of file\n");
  473. }
  474.  
  475. /* Output a line from TEXT up to LIMIT.  Without -t, output verbatim.
  476.    With -t, expand white space characters to spaces, and if FLAG_FORMAT
  477.    is nonzero, output it with argument LINE_FLAG after every
  478.    internal carriage return, so that tab stops continue to line up.  */
  479.  
  480. void
  481. output_1_line (text, limit, flag_format, line_flag)
  482.      char const HUGE *text, HUGE *limit, *flag_format, *line_flag;
  483. {
  484.   if (!tab_expand_flag)
  485.     fwrite (text, sizeof (char), limit - text, outfile);
  486.   else
  487.     {
  488.       register FILE *out = outfile;
  489.       register unsigned char c;
  490.       register char const HUGE *t = text;
  491.       register unsigned column = 0;
  492.  
  493.       while (t < limit)
  494.     switch ((c = *t++))
  495.       {
  496.       case '\t':
  497.         {
  498.           unsigned spaces = TAB_WIDTH - column % TAB_WIDTH;
  499.           column += spaces;
  500.           do
  501.         putc (' ', out);
  502.           while (--spaces);
  503.         }
  504.         break;
  505.  
  506.       case '\r':
  507.         putc (c, out);
  508.         if (flag_format && t < limit && *t != '\n')
  509.           fprintf (out, flag_format, line_flag);
  510.         column = 0;
  511.         break;
  512.  
  513.       case '\b':
  514.         if (column == 0)
  515.           continue;
  516.         column--;
  517.         putc (c, out);
  518.         break;
  519.  
  520.       default:
  521.         if (isprint (c))
  522.           column++;
  523.         putc (c, out);
  524.         break;
  525.       }
  526.     }
  527. }
  528.  
  529. int
  530. change_letter (inserts, deletes)
  531.      int inserts, deletes;
  532. {
  533.   if (!inserts)
  534.     return 'd';
  535.   else if (!deletes)
  536.     return 'a';
  537.   else
  538.     return 'c';
  539. }
  540.  
  541. /* Translate an internal line number (an index into diff's table of lines)
  542.    into an actual line number in the input file.
  543.    The internal line number is LNUM.  FILE points to the data on the file.
  544.  
  545.    Internal line numbers count from 0 starting after the prefix.
  546.    Actual line numbers count from 1 within the entire file.  */
  547.  
  548. int
  549. translate_line_number (file, lnum)
  550.      struct file_data const *file;
  551.      int lnum;
  552. {
  553.   return lnum + file->prefix_lines + 1;
  554. }
  555.  
  556. void
  557. translate_range (file, a, b, aptr, bptr)
  558.      struct file_data const *file;
  559.      int a, b;
  560.      int *aptr, *bptr;
  561. {
  562.   *aptr = translate_line_number (file, a - 1) + 1;
  563.   *bptr = translate_line_number (file, b + 1) - 1;
  564. }
  565.  
  566. /* Print a pair of line numbers with SEPCHAR, translated for file FILE.
  567.    If the two numbers are identical, print just one number.
  568.  
  569.    Args A and B are internal line numbers.
  570.    We print the translated (real) line numbers.  */
  571.  
  572. void
  573. print_number_range (sepchar, file, a, b)
  574.      char sepchar;
  575.      struct file_data *file;
  576.      int a, b;
  577. {
  578.   int trans_a, trans_b;
  579.   translate_range (file, a, b, &trans_a, &trans_b);
  580.  
  581.   /* Note: we can have B < A in the case of a range of no lines.
  582.      In this case, we should print the line number before the range,
  583.      which is B.  */
  584.   if (trans_b > trans_a)
  585.     fprintf (outfile, "%d%c%d", trans_a, sepchar, trans_b);
  586.   else
  587.     fprintf (outfile, "%d", trans_b);
  588. }
  589.  
  590. /* Look at a hunk of edit script and report the range of lines in each file
  591.    that it applies to.  HUNK is the start of the hunk, which is a chain
  592.    of `struct change'.  The first and last line numbers of file 0 are stored in
  593.    *FIRST0 and *LAST0, and likewise for file 1 in *FIRST1 and *LAST1.
  594.    Note that these are internal line numbers that count from 0.
  595.  
  596.    If no lines from file 0 are deleted, then FIRST0 is LAST0+1.
  597.  
  598.    Also set *DELETES nonzero if any lines of file 0 are deleted
  599.    and set *INSERTS nonzero if any lines of file 1 are inserted.
  600.    If only ignorable lines are inserted or deleted, both are
  601.    set to 0.  */
  602.  
  603. void
  604. analyze_hunk (hunk, first0, last0, first1, last1, deletes, inserts)
  605.      struct change *hunk;
  606.      int *first0, *last0, *first1, *last1;
  607.      int *deletes, *inserts;
  608. {
  609.   int l0, l1, show_from, show_to;
  610.   int i;
  611.   int trivial = ignore_blank_lines_flag || ignore_regexp_list;
  612.   struct change *next;
  613.  
  614.   show_from = show_to = 0;
  615.  
  616.   *first0 = hunk->line0;
  617.   *first1 = hunk->line1;
  618.  
  619.   next = hunk;
  620.   do
  621.     {
  622.       l0 = next->line0 + next->deleted - 1;
  623.       l1 = next->line1 + next->inserted - 1;
  624.       show_from += next->deleted;
  625.       show_to += next->inserted;
  626.  
  627.       for (i = next->line0; i <= l0 && trivial; i++)
  628.     if (!ignore_blank_lines_flag || files[0].linbuf[i][0] != '\n')
  629.       {
  630.         struct regexp_list *r;
  631.         char const HUGE *line = files[0].linbuf[i];
  632.         int len = files[0].linbuf[i + 1] - line;
  633.  
  634.         for (r = ignore_regexp_list; r; r = r->next)
  635.           if (0 <= re_search (&r->buf, line, len, 0, len, 0))
  636.         break;    /* Found a match.  Ignore this line.  */
  637.         /* If we got all the way through the regexp list without
  638.            finding a match, then it's nontrivial.  */
  639.         if (!r)
  640.           trivial = 0;
  641.       }
  642.  
  643.       for (i = next->line1; i <= l1 && trivial; i++)
  644.     if (!ignore_blank_lines_flag || files[1].linbuf[i][0] != '\n')
  645.       {
  646.         struct regexp_list *r;
  647.         char const HUGE *line = files[1].linbuf[i];
  648.         int len = files[1].linbuf[i + 1] - line;
  649.  
  650.         for (r = ignore_regexp_list; r; r = r->next)
  651.           if (0 <= re_search (&r->buf, line, len, 0, len, 0))
  652.         break;    /* Found a match.  Ignore this line.  */
  653.         /* If we got all the way through the regexp list without
  654.            finding a match, then it's nontrivial.  */
  655.         if (!r)
  656.           trivial = 0;
  657.       }
  658.     }
  659.   while ((next = next->link) != 0);
  660.  
  661.   *last0 = l0;
  662.   *last1 = l1;
  663.  
  664.   /* If all inserted or deleted lines are ignorable,
  665.      tell the caller to ignore this hunk.  */
  666.  
  667.   if (trivial)
  668.     show_from = show_to = 0;
  669.  
  670.   *deletes = show_from;
  671.   *inserts = show_to;
  672. }
  673.  
  674. /* malloc a block of memory, with fatal error message if we can't do it. */
  675.  
  676. VOID *
  677. xmalloc (size)
  678.      size_t size;
  679. {
  680.   register VOID *value;
  681.  
  682.   if (size == 0)
  683.     size = 1;
  684.  
  685.   value = (VOID *) malloc (size);
  686.  
  687.   if (!value)
  688. #ifdef __MSDOS__
  689.     fatal ("real memory exhausted");
  690. #else
  691.     fatal ("virtual memory exhausted");
  692. #endif
  693.   return value;
  694. }
  695.  
  696. /* realloc a block of memory, with fatal error message if we can't do it. */
  697.  
  698. VOID *
  699. xrealloc (old, size)
  700.      VOID *old;
  701.      size_t size;
  702. {
  703.   register VOID *value;
  704.  
  705.   if (size == 0)
  706.     size = 1;
  707.  
  708.   value = (VOID *) realloc (old, size);
  709.  
  710.   if (!value)
  711. #ifdef __MSDOS__
  712.     fatal ("real memory exhausted");
  713. #else
  714.     fatal ("virtual memory exhausted");
  715. #endif
  716.   return value;
  717. }
  718.  
  719. /* Concatenate three strings, returning a newly malloc'd string.  */
  720.  
  721. char *
  722. concat (s1, s2, s3)
  723.      char const *s1, *s2, *s3;
  724. {
  725.   size_t len = strlen (s1) + strlen (s2) + strlen (s3);
  726.   char *new = xmalloc (len + 1);
  727.   sprintf (new, "%s%s%s", s1, s2, s3);
  728.   return new;
  729. }
  730.  
  731. /* Yield the newly malloc'd pathname
  732.    of the file in DIR whose filename is FILE.  */
  733.  
  734. char *
  735. dir_file_pathname (dir, file)
  736.      char const *dir, *file;
  737. {
  738. #if defined(__MSDOS__) || defined(__NT__)
  739.   char sep = dir[strlen(dir) - 1];
  740.   return concat (dir, "/" + (*dir && ((sep == '/') || (sep == '\\'))), file);
  741. #else
  742.   return concat (dir, "/" + (*dir && dir[strlen (dir) - 1] == '/'), file);
  743. #endif /*__MSDOS__||__NT__*/
  744. }
  745.  
  746. void
  747. debug_script (sp)
  748.      struct change *sp;
  749. {
  750.   fflush (stdout);
  751.   for (; sp; sp = sp->link)
  752.     fprintf (stderr, "%3d %3d delete %d insert %d\n",
  753.          sp->line0, sp->line1, sp->deleted, sp->inserted);
  754.   fflush (stderr);
  755. }
  756.  
  757. #if !HAVE_MEMCHR
  758. char *
  759. memchr (s, c, n)
  760.      char const *s;
  761.      int c;
  762.      size_t n;
  763. {
  764.   unsigned char const *p = (unsigned char const *) s, *lim = p + n;
  765.   for (;  p < lim;  p++)
  766.     if (*p == c)
  767.       return (char *) p;
  768.   return 0;
  769. }
  770. #endif
  771.